package net.bwie.realtime.jtp.Rpfunction;

import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * 简介说明:
 *
 * @author: LiLi
 * @date: 2025/05/28 10:24:51
 * @version: 1.0
 */
public class UserCountWindowFunction implements WindowFunction<Long, String, Integer, TimeWindow> {

    private FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    @Override
    public void apply(Integer integer, TimeWindow window, Iterable<Long> input, Collector<String> out) throws Exception {
        // 获取用户计数
        Long userCount = input.iterator().next();

        String windowStartTime = format.format(window.getStart());
        String windowEndTime = format.format(window.getEnd());
        //格式化输出
        String output = windowStartTime + "," + windowEndTime + "," +integer+"," +userCount +","+
                System.currentTimeMillis();


        out.collect(output);
    }

}